package service;

import dao.BikeProductDao;
import dao.BikeProductDaoImpl;
import entity.BikeProduct;
import org.json.JSONArray;
import org.json.JSONObject;
import utils.DbUtils;

import java.util.List;

/**
 * @author lishunlin110
 * @version 1.0
 * @description: 搜索服务接口实现
 * @date 2021/9/2 16:41
 */
public class SearchServiceImpl implements SearchService{
    @Override
    public JSONArray searchToDisplay(String searchContent) {
        BikeProductDao bikeProductDao = new BikeProductDaoImpl();
        List<BikeProduct> bikeProductList;
        JSONArray jsonArray = new JSONArray();
        try {
            DbUtils.begin();
            bikeProductList = bikeProductDao.queryBySearchContent(searchContent);
            if(bikeProductList!=null){
                for (BikeProduct e:bikeProductList){
                    JSONObject jsonobj = new JSONObject();
                    jsonobj.put("id",e.getP_num());
                    jsonobj.put("brand",e.getBrand());
                    jsonobj.put("bike_name",e.getP_name());
                    jsonobj.put("bike_model",e.getP_model());
                    jsonobj.put("price_range",e.getPrice_range());
                    jsonobj.put("pic_adr",e.getDisplay_pic());
                    jsonArray.put(jsonobj);
                }
                return jsonArray;
            }
            DbUtils.commit();
        }catch (Exception e){
            DbUtils.rollback();
            e.printStackTrace();
        }
        return null;
    }
}
